package nl.saxion.ami;

import java.nio.FloatBuffer;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.khronos.opengles.GL10;

import edu.dhbw.andar.ARObject;
import edu.dhbw.andar.util.GraphicsUtil;

public class SensorWaarde extends ARObject {


	private int sensorid;
	private Activity mainactivity;
	private GLText glText;
	//	private MarkerDrawing drawing;
	private FloatBuffer mat_flash;
	private FloatBuffer mat_ambient;
	private FloatBuffer mat_flash_shiny;
	private FloatBuffer mat_diffuse;
	private String text;
	private String text2;
	private Timer timer;

	public SensorWaarde(String name, String patternName, double markerWidth, double[] markerCenter,
			int sensorid, Activity activity) {
		super(name, patternName, markerWidth, markerCenter);
		this.sensorid = sensorid;
		mainactivity = activity;


		//groen
		//		float   mat_ambientf[]     = {0f, 1.0f, 0f, 1.0f};
		//		float   mat_flashf[]       = {0f, 1.0f, 0f, 1.0f};
		//		float   mat_diffusef[]       = {0f, 1.0f, 0f, 1.0f};
		//		float   mat_flash_shinyf[] = {50.0f};


		//geel
		//		float   mat_ambientf[]     = {1.0f, 1.0f, 0f, 1.0f};
		//		float   mat_flashf[]       = {1.0f, 1.0f, 0f, 1.0f};
		//		float   mat_diffusef[]       = {1.0f, 1.0f, 0f, 1.0f};
		//		float   mat_flash_shinyf[] = {50.0f};

		//oranje
		//		float   mat_ambientf[]     = {1.0f, 0.5f, 0f, 1.0f};
		//		float   mat_flashf[]       = {1.0f, 0.5f, 0f, 1.0f};
		//		float   mat_diffusef[]       = {1.0f, 0.5f, 0f, 1.0f};
		//		float   mat_flash_shinyf[] = {50.0f};

		//rood
		//		float   mat_ambientf[]     = {1.0f, 0f, 0f, 1.0f};
		//		float   mat_flashf[]       = {1.0f, 0f, 0f, 1.0f};
		//		float   mat_diffusef[]       = {1.0f, 0f, 0f, 1.0f};
		//		float   mat_flash_shinyf[] = {50.0f};

		//blauw
		float   mat_ambientf[]     = {0f, 0f, 1.0f, 1.0f};
		float   mat_flashf[]       = {1.0f, 0f, 1.0f, 1.0f};
		float   mat_diffusef[]       = {0f, 0f, 1.0f, 1.0f};
		float   mat_flash_shinyf[] = {50.0f};


		mat_ambient = GraphicsUtil.makeFloatBuffer(mat_ambientf);
		mat_flash = GraphicsUtil.makeFloatBuffer(mat_flashf);
		mat_flash_shiny = GraphicsUtil.makeFloatBuffer(mat_flash_shinyf);
		mat_diffuse = GraphicsUtil.makeFloatBuffer(mat_diffusef);

		//		drawing = new MarkerDrawing();
		text = "";
		text2 = "";
	}

	public int getSensorid(){
		return sensorid;
	}

	public Activity getMainactivity(){
		return mainactivity;
	}

	@Override
	public void init(GL10 gl) {
	}

	public void stopTimer(){
		if(timer !=null){
			timer.cancel();
		}
	}



	@Override
	public final void draw(GL10 gl){

		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR,mat_flash);
		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, mat_flash_shiny);	
		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, mat_diffuse);	
		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, mat_ambient);

		//timer voor ophalen data uit database
		if(glText == null){
			glText = new GLText( gl, mainactivity.getAssets() );
			glText.load( "frutiger.ttf", 12, 0, 0 );  // 9? 40

			timer = new Timer();
			timer.schedule(new TimerTask() {
				public void run()  {

					Sensor s = mainactivity.getWaardeSensor(sensorid);
					if(s != null){
						text = s.getNaam()+ "";
						text2 = s.getWaarde() + ""; //TODO eenheid erachter
					}else {
						text = "Geen verbinding";
						text2 = " / geen sensor gekoppeld";
					}
				}
			}, 1, 3000);
		}


		super.draw(gl);

		// Set to ModelView mode
		gl.glMatrixMode( GL10.GL_MODELVIEW );           // Activate Model View Matrix

		// enable texture + alpha blending
		gl.glEnable( GL10.GL_TEXTURE_2D );              // Enable Texture Mapping
		gl.glEnable( GL10.GL_BLEND );                   // Enable Alpha Blend
		gl.glBlendFunc( GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA );  // Set Alpha Blend Function

		gl.glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );         // Set Color to Use


		glText.begin( 1.0f, 1.0f, 1.0f, 0.0f );         // Begin Text Rendering (Set Color WHITE)

		glText.drawC( text, 0, 0 );          // Draw String
		glText.drawC(text2, -15, -20f); //15 20
		glText.end();                                   // End Text Rendering


		// disable texture + alpha
		gl.glDisable( GL10.GL_BLEND );                  // Disable Alpha Blend
		gl.glDisable( GL10.GL_TEXTURE_2D );             // Disable Texture Mapping

	}


}
